package top.jnaw.jee.utils.nim;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
import top.jnaw.jee.utils.Log;

public class NIMGroup {

  private static final String BASE_URL = "https://api.netease.im/nimserver/team";

  private static final String URL_CREATE = BASE_URL + "/create.action";
  private static final String URL_ADD = BASE_URL + "/add.action";
  private static final String URL_KICK = BASE_URL + "/kick.action";
  private static final String URL_UPDATE = BASE_URL + "/update.action";
  private static final String URL_REMOVE = BASE_URL + "/remove.action";
  private static final String URL_QUERY = BASE_URL + "/queryDetail.action";
  private static final String URL_LIST = BASE_URL + "/joinTeams.action";
  private static final String URL_TRANSFER = BASE_URL + "/changeOwner.action";
  private static final String URL_ADD_ADMINS = BASE_URL + "/addManager.action";
  private static final String URL_REMOVE_ADMINS =
      BASE_URL + "/removeManager.action";
  private static final String URL_LEAVE = BASE_URL + "/leave.action";

  public static String create(String name, String owner, String members) {
    Map<String, String> params = new HashMap<>();
    params.put("tname", name);
    params.put("owner", owner);
    params.put("members", members);
    params.put("msg", "echo");
    params.put("magree", "0");
    params.put("joinmode", "0");
    params.put("beinvitemode", "1");
    params.put("invitemode", "1");
    JSONObject res = Utils.request(URL_CREATE, params);
    return res.getIntValue("code") == 200 ?
           res.getString("tid") : null;
  }

  public static boolean add(String no, String owner, String members) {
    Map<String, String> params = new HashMap<>();
    params.put("tid", no);
    params.put("owner", owner);
    params.put("members", members);
    params.put("msg", "echo");
    params.put("magree", "0");

    JSONObject res = Utils.request(URL_ADD, params);
    return res.getIntValue("code") == 200;
  }

  /**
   * 踢人
   *
   * @param member 一次一个
   */
  public static boolean kick(String no, String owner, String member) {
    Map<String, String> params = new HashMap<>();
    params.put("tid", no);
    params.put("owner", owner);
    params.put("member", member);

    JSONObject res = Utils.request(URL_KICK, params);
    Log.e(res.toJSONString());
    return res.getIntValue("code") == 200;
  }

  public static boolean update(String no, String owner, String name) {
    Map<String, String> params = new HashMap<>();
    params.put("tid", no);
    params.put("owner", owner);
    params.put("tname", name);

    JSONObject res = Utils.request(URL_UPDATE, params);
    return res.getIntValue("code") == 200;
  }

  public static boolean delete(String no, String owner) {
    Map<String, String> params = new HashMap<>();
    params.put("tid", no);
    params.put("owner", owner);

    JSONObject res = Utils.request(URL_REMOVE, params);
    return res.getIntValue("code") == 200;
  }

  public static JSONObject query(String no) {
    Map<String, String> params = new HashMap<>();
    params.put("tid", no);

    JSONObject res = Utils.request(URL_QUERY, params);
    return res.getIntValue("code") == 200 ? res.getJSONObject("tinfo") : null;
  }

  public static JSONArray list(String username) {
    Map<String, String> params = new HashMap<>();
    params.put("accid", username);

    JSONObject res = Utils.request(URL_LIST, params);
    return res.getIntValue("code") == 200 ? res.getJSONArray("infos") : null;
  }

  public static boolean transfer(String no, String owner, String newOwner,
      boolean leave) {
    Map<String, String> params = new HashMap<>();
    params.put("tid", no);
    params.put("owner", owner);
    params.put("newowner", newOwner);
    params.put("leave", leave ? "1" : "2");

    JSONObject res = Utils.request(URL_TRANSFER, params);
    return res.getIntValue("code") == 200;
  }

  /**
   * 添加管理员
   *
   * @param admins 一次最多 10 个
   */
  public static boolean addAdmins(String no, String owner, String admins) {
    Map<String, String> params = new HashMap<>();
    params.put("tid", no);
    params.put("owner", owner);
    params.put("members", admins);

    JSONObject res = Utils.request(URL_ADD_ADMINS, params);
    return res.getIntValue("code") == 200;
  }

  /**
   * 移除管理员
   *
   * @param admins 一次最多 10 个
   */
  public static boolean removeAdmins(String no, String owner,
      String admins) {
    Map<String, String> params = new HashMap<>();
    params.put("tid", no);
    params.put("owner", owner);
    params.put("members", admins);

    JSONObject res = Utils.request(URL_REMOVE_ADMINS, params);
    return res.getIntValue("code") == 200;
  }

  public static boolean setAdmins(String no, String owner, String admins) {
    boolean result = false;

    JSONObject details = shirnkDetail(query(no));
    if (null == details) {
      return result;
    }

    String oldAdmins = details.getJSONArray("admins").toJSONString();

    if (oldAdmins.length() > 2) {
      result = removeAdmins(no, owner, oldAdmins);
    } else {
      result = true;
    }

    if (!result) {
      return result;
    }

    if (admins.length() > 2) {
      result = addAdmins(no, owner, admins);
    } else {
      result = true;
    }

    return result;
  }

  public static boolean leave(String no, String user) {
    Map<String, String> params = new HashMap<>();
    params.put("tid", no);
    params.put("accid", user);

    JSONObject res = Utils.request(URL_LEAVE, params);
    return res.getIntValue("code") == 200;
  }

  public static boolean cleanup(String username) {
    boolean result = false;

    JSONArray array = list(username);
    if (null != array) {
      int size = array.size();
      for (int i = 0; i < size; ++i) {
        JSONObject group = array.getJSONObject(i);
        String no = String.valueOf(group.get("tid"));
        result = delete(no, group.getString("owner"));
      }
    }

    return result;
  }

  public static JSONArray shirnkList(JSONArray list) {
    JSONArray liteArray = new JSONArray();

    int size = list.size();
    for (int i = 0; i < size; ++i) {
      liteArray.add(list.getJSONObject(i).get("tid"));
    }

    return liteArray;
  }

  public static JSONObject shirnkDetail(JSONObject detail) {
    JSONObject tiny = new JSONObject();
    tiny.put("no", detail.get("tid"));
    tiny.put("name", detail.get("tname"));
    tiny.put("count", detail.get("count"));
    tiny.put("owner", detail.getJSONObject("owner").get("accid"));

    JSONArray liteArray = new JSONArray();
    JSONArray array = detail.getJSONArray("members");

    int size = array.size();
    for (int i = 0; i < size; ++i) {
      liteArray.add(array.getJSONObject(i).get("accid"));
    }

    tiny.put("members", liteArray);

    liteArray = new JSONArray();
    array = detail.getJSONArray("admins");

    size = array.size();
    for (int i = 0; i < size; ++i) {
      liteArray.add(array.getJSONObject(i).get("accid"));
    }

    tiny.put("admins", liteArray);

    return tiny;
  }
}
